<template>
  <div class="login-container">
    <!-- 左侧特效区域 -->
    <div class="left-panel">
      <div class="gradient-bg"></div>
      <div class="particles">
        <div class="particle" v-for="i in 20" :key="i" :style="getParticleStyle()"></div>
      </div>
      <div class="content">
        <h1 class="title">慧影AI后台管理</h1>
        <p class="subtitle">智能创作 · 数据驱动 · 高效管理</p>
      </div>
    </div>

    <!-- 右侧登录区域 -->
    <div class="right-panel">
      <div class="login-form-container">
        <div class="logo-section">
          <img
            src="https://b.zmxai.com/pc/assets/logo_layout-DkcrJCB-.png"
            alt="Logo"
            class="logo"
          />
        </div>

        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <input
              v-model="loginForm.account"
              type="text"
              placeholder="用户名"
              class="form-input"
              required
            />
          </div>

          <div class="form-group">
            <input
              v-model="loginForm.password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="密码"
              class="form-input"
              required
            />
            <button type="button" class="password-toggle" @click="showPassword = !showPassword">
              <el-icon>
                <View v-if="showPassword" />
                <Hide v-else />
              </el-icon>
            </button>
          </div>

          <button type="submit" class="login-button">
            <span>登录</span>
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { View, Hide } from '@element-plus/icons-vue'
import { login } from '@/api/auth'
import { useUserStore } from '@/store/auth'
import { loadRoutesFromAPI } from '@/router/dynamic'
const router = useRouter()
const authStore = useUserStore()
const showPassword = ref(false)

const loginForm = reactive({
  account: '',
  password: '',
})

const handleLogin = async () => {
  if (!loginForm.account || !loginForm.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  const res: any = await login(loginForm)
  if (res.code === 200) {
    ElMessage.success('登录成功')
    // 保存token
    authStore.setToken(res.data.token)
    authStore.setUserInfo(res.data)
    await loadRoutesFromAPI()
    // 跳转到首页
    router.push('/dashboard')
  }
}

const getParticleStyle = () => {
  const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57', '#ff9ff3', '#54a0ff']
  return {
    left: Math.random() * 100 + '%',
    top: Math.random() * 100 + '%',
    backgroundColor: colors[Math.floor(Math.random() * colors.length)],
    animationDelay: Math.random() * 2 + 's',
    animationDuration: Math.random() * 3 + 2 + 's',
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* 左侧特效面板 */
.left-panel {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.gradient-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  animation: gradientShift 8s ease infinite;
}

@keyframes gradientShift {
  0%,
  100% {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  }
  50% {
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
  }
}

.particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.particle {
  position: absolute;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  animation: float 3s ease-in-out infinite;
  opacity: 0.7;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.7;
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
    opacity: 1;
  }
}

.content {
  position: relative;
  z-index: 10;
  text-align: center;
  color: white;
}

.title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 16px;
  animation: fadeInUp 1s ease-out;
}

.subtitle {
  font-size: 20px;
  opacity: 0.9;
  animation: fadeInUp 1s ease-out 0.3s both;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 右侧登录面板 */
.right-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
}

.login-form-container {
  background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
  padding: 48px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(255, 255, 255, 0.8);
  width: 100%;
  max-width: 400px;
  animation: slideIn 0.6s ease-out;
  border: 1px solid rgba(255, 255, 255, 0.8);
  position: relative;
  overflow: hidden;
}

.login-form-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 50%, rgba(255,255,255,0) 100%);
}

.login-form-container::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.3) 0%, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, 0) 60%, rgba(255, 255, 255, 0.1) 100%);
  pointer-events: none;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(50px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.logo-section {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  border-radius: 12px;
  margin-bottom: 16px;
}



.login-form {
  width: 100%;
}

.form-group {
  position: relative;
  margin-bottom: 24px;
}

.form-input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #ffffff 0%, #f3f4f6 100%);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05), inset 0 1px 0 rgba(255, 255, 255, 0.7);
  position: relative;
  z-index: 1;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 3px 8px rgba(102, 126, 234, 0.15), inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.password-toggle {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 4px;
  transition: color 0.3s ease;
}

.password-toggle:hover {
  color: #667eea;
}

.login-button {
  width: 100%;
  padding: 14px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 3px 12px rgba(102, 126, 234, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 50%, rgba(255,255,255,0) 100%);
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  display: inline-block;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .left-panel {
    display: none;
  }

  .right-panel {
    padding: 20px;
  }

  .login-form-container {
    margin: 20px;
    padding: 32px 24px;
  }
}
</style>
